package gk.gkapi.misc.sample;

import gk.gkapi.DB;
import gk.gkapi.DBMap;
import gk.gkapi.impl.db.hashmap.DBHashMapImpl;


public class ProductBuilder {

	public static void main(String[] args) {
		DB db = new DBHashMapImpl();
		DBMap<Integer, Product> collection = db.getDBCollection("products", Integer.class, Product.class);

		long stime = System.currentTimeMillis();

		for (int i = 0; i <= 1000000; ++i) {
			Product product = new Product(i);
			product.addSku(new Sku("Red Apple", i*10));
			product.addSku(new Sku("Red Apple", i*10));
			//product.addSku(new Sku("Green Apple", i));
			collection.put(product.getCode(), product);
		}

		long etime = System.currentTimeMillis();
		System.out.println("Insert Time:" + (etime - stime));

		stime = System.currentTimeMillis();
		 DBMap<Integer, Product> fcollection =
		 collection.filter("{ \"$or\":[{\"code\": \"5\"}, {\"skus\": {\"price\":{\"$eq\":\"60\"}}}]}");
		//DBMap<Integer, Product> fcollection =
		 //collection.filter("{ \"$or\":[{\"skus\": {\"price\":{\"$eq\":\"50\"}}}, {\"skus\": {\"price\":{\"$eq\":\"60\"}}}]}");
		//DBMap<Integer, Product> fcollection =
		//collection.filter("[{\"skus\": {\"$le\":\"2\"}}]");
		//DBMap<Integer, Product> fcollection =
		//		collection.filter("[{\"code\": {\"$in\":[\"5\", \"10\", \"100\"]}}]");
		//DBMap<Integer, Product> fcollection = collection
		//		.filter("[{\"skus\": {\"price\":{\"$in\":[\"2\", \"4\"]}, \"code\":\"Red Apple\"}}]");
		//DBMap<Integer, Product> fcollection =
		//			collection.filter("{\"code\":\"5\"}");
		etime = System.currentTimeMillis();
		for (int k : fcollection.keySet()) {
			System.out.println(fcollection.get(k));
		}

		System.out.println("Filter Time:" + (etime - stime));

		stime = System.currentTimeMillis();
		fcollection = fcollection.filter("{\"code\": {\"$le\":\"5\"}}");
		etime = System.currentTimeMillis();
		for (int k : fcollection.keySet()) {
			System.out.println(fcollection.get(k));
		}
		System.out.println("Filter Time:" + (etime - stime));

	}
}
